package com.edfu.core.commons.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.edfu.core.commons.persistence.entity.ResourceEntity;
import com.edfu.core.commons.persistence.entity.ResourceMappingEntity;
import com.edfu.core.testmgt.vo.Resource;

public class ResourceMgtBeanTransformer {
	
	public static Resource transformResource(ResourceEntity resourceEntity, List<ResourceMappingEntity> resourceMappings) {
		Resource resource = new Resource();
		resource.setDescription(resourceEntity.getDescription());
		resource.setId(resourceEntity.getId());
//		resource.setInputStream(content.getContentStream());
		resource.setName(resourceEntity.getName());
		resource.setType(resourceEntity.getType());
		resource.setUuid(resourceEntity.getUuid());
		resource.setExposed(resourceEntity.isExposed());
		
		resource.setCategory(CategoryMgtBeanTransformer.transformCategory(resourceEntity.getCategory()));
		
		if (resourceMappings != null && resourceMappings.size() > 0) {
			Map<Long, String> parentMap = new HashMap<Long, String>();
			for (ResourceMappingEntity rme : resourceMappings) {
				parentMap.put(rme.getObjectId(), rme.getObjectType());
			}
			resource.setParentMapping(parentMap);
		}
		
		return resource;
	}
	
	public static ResourceEntity transformResource(Resource resource) {
		ResourceEntity resourceEntity = new ResourceEntity();
		resourceEntity.setDescription(resource.getDescription());
		resourceEntity.setName(resource.getName());
		resourceEntity.setType(resource.getType());
		resourceEntity.setExposed(resource.isExposed());
		
		return resourceEntity;
	}

	public static List<Resource> transformResource(List<ResourceMappingEntity> resourceMappingEntities) {
		List<Resource> resources = new ArrayList<Resource>();
		
		for (ResourceMappingEntity resourceMappingEntity : resourceMappingEntities) {
			resources.add(transformResource(resourceMappingEntity.getResource(), null));
		}
		return resources;
	}

}
